package com.jinlejia.erp.ui.mine.sign;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.utils.DateUtil;
import com.jinlejia.erp.utils.StringUtil;

import java.util.Map;

/**
 * Created by Administrator on 2018/12/20 0020.
 */

public class DialogSignState extends AlertDialog {
    ImageView close;
    TextView noSignIn;
    TextView timeSignIn;
    TextView isFieldSignIn;
    TextView signInState;
    RelativeLayout signInInfo;
    TextView noSignOut;
    TextView timeSignOut;
    TextView isFieldSignOut;
    TextView signOutState;
    RelativeLayout signOutInfo;
    private Context mContext;

    public DialogSignState(Context context) {
        super(context);
        mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setCanceledOnTouchOutside(false);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.dialog_sign_state);
        window.setGravity(Gravity.CENTER);
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawable(new ColorDrawable());
        setCanceledOnTouchOutside(true);
        close = window.findViewById(R.id.close);
        noSignIn = window.findViewById(R.id.no_sign_in);
        timeSignIn = window.findViewById(R.id.time_sign_in);
        isFieldSignIn = window.findViewById(R.id.is_field_sign_in);
        signInState = window.findViewById(R.id.sign_in_state);
        signInInfo = window.findViewById(R.id.sign_in_info);
        noSignOut = window.findViewById(R.id.no_sign_out);
        timeSignOut = window.findViewById(R.id.time_sign_out);
        isFieldSignOut = window.findViewById(R.id.is_field_sign_out);
        signOutState = window.findViewById(R.id.sign_out_state);
        signOutInfo = window.findViewById(R.id.sign_out_info);

        window.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public void setData(JSONArray array) {
        for (int i = 0; i < array.size(); i++) {
            final JSONObject object = (JSONObject) array.get(i);
            if (object.getIntValue("type") == 1) {
                //上班
                switch (object.getIntValue("signState")) {
                    case 0:
                        //未签到
                        noSignIn.setText("未签到");
                        signInInfo.setVisibility(View.GONE);
                        break;
                    case 1:
                        //已签到
                        noSignIn.setVisibility(View.GONE);
                        signInInfo.setVisibility(View.VISIBLE);
                        timeSignIn.setText(DateUtil.getTime(object.getString("signTime"), "HH:mm"));
                        signInState.setText("已签到");
                        break;
                    case 2:
                        //迟到
                        noSignIn.setVisibility(View.GONE);
                        signInInfo.setVisibility(View.VISIBLE);
                        timeSignIn.setText(DateUtil.getTime(object.getString("signTime"), "HH:mm"));
                        signInState.setText("迟到");
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
                if (object.getIntValue("signType") == 1) {
                    //正常
                    isFieldSignIn.setVisibility(View.GONE);
                } else if (object.getIntValue("signType") == 2) {
                    //外勤
                    isFieldSignIn.setVisibility(View.VISIBLE);
                    isFieldSignIn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, DetailFieldSignInActivity.class);
                            intent.putExtra("id", object.getString("id"));
                            mContext.startActivity(intent);
                        }
                    });
                }
            } else if (object.getIntValue("type") == 2) {
                //下班
                switch (object.getIntValue("signState")) {
                    case 0:
                        //未签退
                        noSignOut.setText("未签退");
                        signOutInfo.setVisibility(View.GONE);
                        break;
                    case 1:
                        //已签退
                        noSignOut.setVisibility(View.GONE);
                        signOutInfo.setVisibility(View.VISIBLE);
                        timeSignOut.setText(DateUtil.getTime(object.getString("signTime"), "HH:mm"));
                        signOutState.setText("已签退");
                        break;
                    case 2:
                        //迟到
                        break;
                    case 3:
                        //早退
                        noSignOut.setVisibility(View.GONE);
                        signOutInfo.setVisibility(View.VISIBLE);
                        timeSignOut.setText(DateUtil.getTime(object.getString("signTime"), "HH:mm"));
                        signOutState.setText("早退");
                        break;
                    default:
                        break;
                }
                if (object.getIntValue("signType") == 1) {
                    //正常
                    isFieldSignOut.setVisibility(View.GONE);
                } else if (object.getIntValue("signType") == 2) {
                    //外勤
                    isFieldSignOut.setVisibility(View.VISIBLE);
                    isFieldSignOut.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, DetailFieldSignInActivity.class);
                            intent.putExtra("id", object.getString("id"));
                            mContext.startActivity(intent);
                        }
                    });
                }
            }
        }
    }

    public void setData(Map<String, Object> map) {
        System.out.println(map.entrySet());
        //上班
        if (!map.containsKey("upperSignState")) {
            noSignIn.setText("未签到");
            signInInfo.setVisibility(View.GONE);
        } else {
            switch (Integer.parseInt(StringUtil.valueOf(map.get("upperSignState")))) {
                case 0:
                    noSignIn.setText("未签到");
                    signInInfo.setVisibility(View.GONE);
                    break;
                case 1:
                    noSignIn.setVisibility(View.GONE);
                    signInInfo.setVisibility(View.VISIBLE);
                    timeSignIn.setText(DateUtil.getTime(StringUtil.valueOf(map.get("upperSignTime")), "HH:mm"));
                    signInState.setText("已签到");
                    break;
                case 2:
                    noSignIn.setVisibility(View.GONE);
                    signInInfo.setVisibility(View.VISIBLE);
                    timeSignIn.setText(DateUtil.getTime(StringUtil.valueOf(map.get("upperSignTime")), "HH:mm"));
                    signInState.setText("迟到");
                    break;
                default:
                    break;
            }
        }

        //外勤
        isFieldSignIn.setVisibility(View.GONE);


        //下班
        if (!map.containsKey("lowerSignState")) {
            noSignOut.setText("未签退");
            signOutInfo.setVisibility(View.GONE);
        } else {
            switch (Integer.parseInt(StringUtil.valueOf(map.get("lowerSignState")))) {
                case 0:
                    noSignOut.setText("未签退");
                    signOutInfo.setVisibility(View.GONE);
                    break;
                case 1:
                    //已签退
                    noSignOut.setVisibility(View.GONE);
                    signOutInfo.setVisibility(View.VISIBLE);
                    timeSignOut.setText(DateUtil.getTime(StringUtil.valueOf(map.get("lowerSignTime")), "HH:mm"));
                    signOutState.setText("已签退");
                    break;
                case 2:
                    break;
                case 3:
                    noSignOut.setVisibility(View.GONE);
                    signOutInfo.setVisibility(View.VISIBLE);
                    timeSignOut.setText(DateUtil.getTime(StringUtil.valueOf(map.get("lowerSignTime")), "HH:mm"));
                    signOutState.setText("早退");
                    break;
                default:
                    break;
            }
        }
        //外勤
        isFieldSignOut.setVisibility(View.GONE);

    }
}
